package com.envper.common.service.device.impl;

import com.envper.common.domain.dto.DeviceLogDto;
import com.envper.common.mapper.DeviceMapper;
import com.envper.common.service.device.DeviceLogService;
import com.envper.mysql.base.service.DeviceLogBaseService;
import com.envper.mysql.domain.entity.TDeviceLog;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DeviceLogServiceImpl implements DeviceLogService {
    private final DeviceLogBaseService deviceLogBaseService;

    @Override
    public List<DeviceLogDto> getDeviceLogList() {
        List<TDeviceLog> tDeviceLogList = deviceLogBaseService.list();
        return DeviceMapper.INSTANCE.toDeviceLogDtoList(tDeviceLogList);
    }

    @Override
    public List<DeviceLogDto> getDeviceLogListByDeviceId(String deviceId, Long limit) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("device_id", deviceId);
        queryWrapper.limit(limit);
        queryWrapper.orderBy("upload_time", false);
        List<TDeviceLog> tDeviceLogList = deviceLogBaseService.list(queryWrapper);
        return DeviceMapper.INSTANCE.toDeviceLogDtoList(tDeviceLogList);
    }
}
